<template>
  <div class="big-box">
    <el-row>
      <el-form ref="carList" class="top" :model="carList" :rules="carListRules">
        <el-row>
          <el-col :span="8">
            <el-form-item label="车辆编号" label-width="100px" prop="truckTypeId">
              <el-input v-if="isShow" v-model="carList.truckTypeId" disabled style="width: 300px" />
              <el-button v-else disabled type="text" style="color: #20232a">{{ carList.truckTypeId }}</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆号码" label-width="100px" prop="licensePlate">
              <el-input v-if="isShow" v-model="carList.licensePlate" style="width: 300px" />
              <el-button v-else disabled type="text" style="color: #20232a">{{ carList.licensePlate }}</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆名称" label-width="100px" prop="truckTypeName">
              <el-select
                v-if="isShow"
                v-model="carList.truckTypeName"
                placeholder="请选择"
                style="width: 300px"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
              <el-button v-else disabled type="text" style="color: #20232a">{{ carList.truckTypeName }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="车辆体积" label-width="100px" prop="allowableVolume">
              <el-input v-if="isShow" v-model="carList.allowableVolume" style="width: 300px" />
              <el-button v-else disabled type="text" style="color: #20232a">{{ carList.allowableVolume }}</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆载重" label-width="100px" prop="allowableLoad">
              <el-input v-if="isShow" v-model="carList.allowableLoad" style="width: 300px" />
              <el-button
                v-else
                type="text"
                style="color: #20232a"
                disabled
              >{{ carList.allowableLoad }}吨</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="GPSID" label-width="100px" prop="deviceGpsId">
              <el-input v-if="isShow" v-model="carList.deviceGpsId" style="width: 300px" />
              <el-button v-else disabled type="text" style="color: #20232a">{{ carList.deviceGpsId }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="图片信息" label-width="100px" prop="picture">
              <div v-if="isShow === false" style="display: flex;">
                <div
                  style="
                    width: 212px;
                    height: 159px;
                    border: 1px solid rgb(216, 221, 227);
                    border-radius: 4px;
                    margin-right: 20px;"
                >
                  <img
                    style="width:212px;height: 159px;"
                    src="https://yjy-slwl-oss.oss-cn-hangzhou.aliyuncs.com/61032c27-43eb-49cc-86df-ba8cbeb2c27d.png"
                    alt=""
                  >
                </div>
                <div
                  style="
                    width: 212px;
                    height: 159px;
                    border: 1px solid rgb(216, 221, 227);
                    border-radius: 4px;
                    margin-right: 20px;"
                >
                  <img
                    style="width:212px;height: 159px;"
                    src="https://yjy-slwl-oss.oss-cn-hangzhou.aliyuncs.com/3b59ab0c-59fc-4c96-a645-eda33696204b.png"
                    alt=""
                  >
                </div>
              </div>
              <div v-if="isShow" style="display:flex">
                <div
                  style="
                    width: 212px;
                    height: 159px;
                    border: 1px solid rgb(216, 221, 227);
                    border-radius: 4px;
                    margin-right: 20px;"
                >
                  <img
                    style="width:212px;height: 159px;"
                    src="https://yjy-slwl-oss.oss-cn-hangzhou.aliyuncs.com/61032c27-43eb-49cc-86df-ba8cbeb2c27d.png"
                    alt=""
                  >
                </div>
                <div
                  style="
                    width: 212px;
                    height: 159px;
                    border: 1px solid rgb(216, 221, 227);
                    border-radius: 4px;
                    margin-right: 20px;"
                >
                  <img
                    style="width:212px;height: 159px;"
                    src="https://yjy-slwl-oss.oss-cn-hangzhou.aliyuncs.com/3b59ab0c-59fc-4c96-a645-eda33696204b.png"
                    alt=""
                  >
                </div>
                <div>
                  <el-upload
                    action=""
                    list-type="picture-card"
                  >
                    <i class="el-icon-upload2" />
                  </el-upload>
                  <span
                    style="font-size: 12px"
                  >注：图片大小不超过5M；仅能上传 PNG JPEG
                    JPG类型图片；建议上传400*300尺寸的图片；最多上传2张</span>
                </div>
              </div>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label-width="100px">
              <template v-if="isShow">
                <el-button type="primary" @click="submit">保存</el-button>
                <el-button @click="isShow = false">取消</el-button>
              </template>
              <template v-else>
                <el-button type="primary" @click="isShow = true">编辑</el-button>
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </div>
</template>

<script>
// import { getCarParticulars, getCarList, updateCarParticulars } from '@/api/truck'
import { getCarParticulars, getCarList } from '@/api/truck'
export default {
  data() {
    return {
      options: [], // 获取车辆类型列表
      carList: {},
      isShow: false, // 点击编辑就进入编辑模式
      dialogImageUrl: '',
      dialogVisible: false,
      // 规则
      carListRules: {
        truckTypeId: [
          { required: true, message: '车辆编号不能为空', trigger: 'blur' }
        ],
        licensePlate: [
          { required: true, message: '车辆号码不能为空', trigger: 'blur' }
        ],
        truckTypeName: [
          { required: true, message: '车型名称不能为空', trigger: 'blur' }
        ],
        allowableVolume: [
          { required: true, message: '车辆体积不能为空', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '车辆载重不能为空', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: 'GPSID不能为空', trigger: 'blur' }
        ],
        picture: [
          { required: true, message: '车辆图片最少要有一张', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取车辆详情
    this.getCarParticulars()
    // 获取车辆类型列表
    const res = await getCarList()
    this.options = res.data
  },
  methods: {
    // 获取车辆详情
    async getCarParticulars() {
      const res = await getCarParticulars(this.$route.params.id)
      this.carList = res.data
    },
    // 点击放大镜就放大图片
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 点击保存
    async submit() {
      this.$message.error('初始化数据不能修改')
      // await this.$refs.carList.validate()
      // const res = await updateCarParticulars(this.$route.params.id)
      // console.log(res)
    }
  }
}
</script>

<style scoped lang="scss">
.big-box {
  background-color: #fff;
  height: 400px;

}
.top {
  margin-top: 20px;
}
.el-col {
  display: flex;
  justify-content: center;
}

</style>
